package com.xmb.wpclient.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xmb.wpclient.cache.CommonCache;
import com.xmb.wpclient.constant.BaseEntry;
import com.xmb.wpclient.entity.*;
import com.xmb.wpclient.service.IBaseCommonConstantService;
import com.xmb.wpclient.service.ISysMenuService;
import com.xmb.wpclient.service.ISysRolePermissionService;
import com.xmb.wpclient.utils.BeanUtil;
import com.xmb.wpclient.utils.ShiroUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;
import java.util.stream.Collectors;

@Controller
public class IndexController{
	
	@Autowired
	private ISysMenuService iSysMenuService;
	
	@Autowired
	private ISysRolePermissionService iSysRolePermissionService;

	@Value("${server.name}")
	private String systemName;

	@Autowired
	private IBaseCommonConstantService iBaseCommonConstantService;

	@Autowired
	private CommonCache commonCache;

	@RequestMapping("/index")
	public String index(Model model) {
		//获取当前用户
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		//根据当前用户角色查询拥有权限
		List<SysRolePermission> permissionList = iSysRolePermissionService.selectInfoList(new QueryWrapper<SysRolePermission>().eq("role_id", user.getRoleId()));
		Set<String> permissionIds = new HashSet<String>();
		for (SysRolePermission sysRolePermission : permissionList) {
			permissionIds.add(sysRolePermission.getPermissionId());
		}
		QueryWrapper<SysMenu> ew = new QueryWrapper<SysMenu>();
		ew.and(wrapper -> wrapper.isNull("supper_id").or().eq("supper_id", ""));
		ew.in("id", permissionIds);
		if(permissionIds.size()==0) {
			ew.select("1!=1");
		}
		model.addAttribute("list", iSysMenuService.list(ew.orderByAsc("order_by_index")));
		user.setUserPassword(null);
		model.addAttribute("user", user);
		model.addAttribute("systemName", systemName);
		return "index";
	}

	public List<Map> dataTransfer(List<Object> objList, List<BaseCommonDataDictionaryAndSet> collect) throws Exception {
		List<Map> resultList = new ArrayList<>();
		for(Object o : objList){
			Map<String, String> map = BeanUtils.describe(o);
			BeanUtil.dateFormat(o, map);
			resultList.add(map);
		}
		for(BaseCommonDataDictionaryAndSet set : collect){
			BaseCommonConstant constant = iBaseCommonConstantService.getOne(new LambdaQueryWrapper<BaseCommonConstant>().eq(BaseCommonConstant::getId, set.getQueryConstantId()));
			BaseEntry entry = commonCache.init(constant.getUrl());
			IService<Object> constrastService = entry.getIService();
			for(Map map : resultList){
				Object contrastObj = constrastService.getOne(new QueryWrapper<>().eq(set.getAssociatedFiled(), map.get(set.getEntityField()))
						.or().eq(set.getQueryTableList().get(0).getQueryField(), map.get(set.getEntityField())));
				for(BaseCommonQueryTable queryTable : set.getQueryTableList()){
					map.put(set.getEntityField()+"_"+queryTable.getQueryField(),contrastObj == null?"":BeanUtil.getMethod(contrastObj, StrUtil.toCamelCase(queryTable.getQueryField())));
				}
			}
		}
		return resultList;
	}
}